package com.foo.cms.action;

import com.foo.cms.reqvo.ArticleListReq;
import com.foo.cms.service.*;
import com.foo.cms.vo.Article;
import com.foo.cms.vo.Pager;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: LZS
 * Date: 12-6-23
 * Time: 下午1:23
 * To change this template use File | Settings | File Templates.
 */
@Controller
@RequestMapping("/article")
public class ArticleAction {
    private static final Logger logger = LoggerFactory.getLogger(ArticleAction.class);

    @Resource
    private ArticleService articleService;

    @Resource
    private CategoryService categoryService;

    @Resource
    private PartService partService;

    @Resource
    private RecommendService recommendService;

    @Resource
    private KeyworkService keyworkService;

    @Resource
    private ArticleRelatedService articleRelatedService;

    @Resource
    private SourceService sourceService;

    @RequestMapping("/index")
    public ModelAndView index() {
        ModelAndView modelAndView = new ModelAndView("article/index.jsp");
        Map<Object, Object> recommends = recommendService.listRec();
        modelAndView.addObject("recommends", recommends);
        modelAndView.addObject("parts", partService.listLeaf());
        return modelAndView;
    }

    @RequestMapping("/list")
    @ResponseBody
    public Pager list(ArticleListReq req) {
        return articleService.listArticle(req);
    }

    @RequestMapping("/del")
    @ResponseBody
    public String del(String id) {
        articleService.deleteArticle(id);
        return "OK";
    }

    @RequestMapping("/editsort")
    @ResponseBody
    public String editsort(String id, String sort) {
        articleService.editSort(id, sort);
        return "OK";
    }

    @RequestMapping("/exam")
    @ResponseBody
    public String exam(String ids, String status) {
        articleService.examArticle(ids, status);
        return "OK";
    }

    @RequestMapping("/recommend")
    @ResponseBody
    public String recommend(String articleIds, String recommendId) {
        articleService.recommendArticle(articleIds, recommendId);
        return "OK";
    }

    @RequestMapping("/edit")
    public ModelAndView edit(String id) {
        ModelAndView modelAndView = new ModelAndView();
        if (StringUtils.isNotBlank(id)) {
            Article article = articleService.getArticle(id);
            String type = article.getType();
            if (StringUtils.isNotBlank(type)) {
                if (type.equals("P")) {
                    modelAndView.setViewName("/picture/edit.jsp");
                } else if (type.equals("A")) {
                    modelAndView.setViewName("/text/edit.jsp");
                    modelAndView.addObject("articleRelated", articleRelatedService.list(id));
                    modelAndView.addObject("categories", categoryService.listAll());
                    modelAndView.addObject("categoriesMap", categoryService.listByArticleId(id));
                } else if (type.equals("I")) {
                    modelAndView.addObject("articleRelated", articleRelatedService.list(id));
                    modelAndView.addObject("sources", sourceService.list(id));
                    modelAndView.setViewName("/image/edit.jsp");
                } else if (type.equals("V")) {
                    modelAndView.setViewName("/video/edit.jsp");
                    modelAndView.addObject("articleRelated", articleRelatedService.list(id));
                }
            }
            modelAndView.addObject("article", article);
            modelAndView.addObject("parts", partService.listLeaf());
            modelAndView.addObject("recommends", recommendService.listRec());
            modelAndView.addObject("recommendMap", recommendService.list(id));
            modelAndView.addObject("keyword", keyworkService.getKeywork(id));
        } else {
            //TODO id is not null
        }
        return modelAndView;
    }

}
